package controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import annotation.Log;
import model.TDimensionOption;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import persistent.factory.DAOFactory;
import util.toolkit.Constant;
import util.toolkit.Expression;
import util.toolkit.ResponseJSON;

/**
 * 维度选项信息处理类
 * @author 徐震霖
 * @since 2019-03-05
 */
@Controller
@RequestMapping(value="/dimensionoption")
public class SysDimensionOptionManagementController {
	
	/**
	 * 添加一个维度选项信息
	 * @param model
	 * @param req
	 * @param resp
	 * @return
	 * @throws IOException
	 */
	//TODO 添加一个题目选项信息
	@Log(operationType = Constant.OPERTATION_ADD, operationDescribe = "添加一个维度选项信息", 
			operationClassPath = "controller.SysDimensionOptionManagementController.addDimensionOption(Model model, HttpServletRequest req,HttpServletResponse resp)", 
			operationMenu = "量表维度管理/维度信息管理")
	@RequestMapping(value="/adddimensionoption")
	public String addDimensionOption(Model model, HttpServletRequest req,
			HttpServletResponse resp) throws IOException {
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("application/json");
		
		JSONObject result = new JSONObject();
		
		try {
			String optionNumber = req.getParameter("optionnumber");     //选项排序号
			String optionContent = req.getParameter("optioncontent");   //选项内容
			String optionScore = req.getParameter("optionscore");      //选项分数
			String dimensionid = req.getParameter("dimensionid");      //维度id
			
			if (optionNumber != null && !optionNumber.equals("") && !optionNumber.equals("0") && optionContent != null && 
					optionScore != null && !optionScore.equals("") && dimensionid != null && !dimensionid.equals("") 
					&& !dimensionid.equals("0")) {
				TDimensionOption option = new TDimensionOption();
				
				option.setOptionNumber(Integer.parseInt(optionNumber));
				option.setOptionContent(optionContent);
				option.setOptionScore(Integer.parseInt(optionScore));
				option.setDimensionid(Integer.parseInt(dimensionid));
				
				if(DAOFactory.getIDimensionOptionDAO().addOption(option)){
					result.put("code", ResponseJSON.FLAG_SUCC);
					result.put("msg", "选项信息添加成功！");
				}
				else {
					result.put("code", ResponseJSON.FLAG_FAIL);
					result.put("msg", "出现错误，选项信息添加失败，请重试！");
				}
			}
			else {
				result.put("code", ResponseJSON.FLAG_FAIL);
				result.put("msg", "必填项不能为空，选项信息添加失败！");
			}
		} catch (Exception e) {
			result.put("code", ResponseJSON.FLAG_FAIL);
			result.put("msg", "出现异常，选项信息添加失败，请重试！");
			e.printStackTrace();
		}
		
		PrintWriter out = resp.getWriter();
		out.println(result);
		return null;
	}
	
	/**
	 * 删除维度选项信息处理类
	 * @param model
	 * @param req
	 * @param resp
	 * @return
	 * @throws IOException
	 */
	//TODO 删除选项信息处理类
	@Log(operationType = Constant.OPERTATION_DELETE, operationDescribe = "删除维度选项信息", 
			operationClassPath = "controller.SysDimensionOptionManagementController.deleteDimensionOption(Model model, HttpServletRequest req,HttpServletResponse resp)", 
			operationMenu = "量表维度管理/维度信息管理")
	@RequestMapping(value="/deletedimensionoption")
	public String deleteDimensionOption(Model model, HttpServletRequest req,
			HttpServletResponse resp) throws IOException {
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("application/json");
		
		JSONObject result = new JSONObject();
		
		try {
			String optionid = req.getParameter("optionid");
			if (optionid != null && !optionid.equals("")) {
				if(DAOFactory.getIDimensionOptionDAO().deleteOption(optionid)){
					result.put("code", ResponseJSON.FLAG_SUCC);
					result.put("msg", "选项信息删除成功！");
				}
				else {
					result.put("code", ResponseJSON.FLAG_FAIL);
					result.put("msg", "出现错误，选项信息删除失败，请重试！");
				}
			}
			else {
				result.put("code", ResponseJSON.FLAG_FAIL);
				result.put("msg", "选项编号不能为空，选项信息删除失败，请重试！");
			}
		} catch (Exception e) {
			result.put("code", ResponseJSON.FLAG_FAIL);
			result.put("msg", "出现异常，选项信息删除失败，请重试！");
			e.printStackTrace();
		}
		
		PrintWriter out = resp.getWriter();
		out.println(result);
		return null;
	}
	
	/**
	 * 根据维度编号获取选项信息
	 * @param model
	 * @param req
	 * @param resp
	 * @return
	 * @throws IOException
	 */
	//TODO 根据题目编号获取选项信息
	@Log(operationType = Constant.OPERTATION_QUERY, operationDescribe = "根据维度编号获取选项信息", 
			operationClassPath = "controller.SysDimensionOptionManagementController.getOptionByDimension(Model model, HttpServletRequest req,HttpServletResponse resp)", 
			operationMenu = "量表维度管理/维度信息管理")
	@RequestMapping(value="/getoptionbydimension")
	public String getOptionByDimension(Model model, HttpServletRequest req,
			HttpServletResponse resp) throws IOException {
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("application/json");
		
		JSONObject result = new JSONObject();
		Expression exp = new Expression();
		
		try {
			String dimensionid = req.getParameter("dimensionid");
			if (dimensionid != null && !dimensionid.equals("") && !dimensionid.equals("0")) {
				exp.andEqu("dimensionid", dimensionid, String.class);
			}
			exp.orderByAsc("optionNumber");
			List<TDimensionOption> options = DAOFactory.getIDimensionOptionDAO().getOptions(exp.toString());
			if (options != null && options.size() > 0) {
				result.put("code", 0);
				result.put("msg", "查询成功");
				result.put("count", options.size());
				JSONArray jsonArray = JSONArray.fromObject(options);
				result.element("data", jsonArray);
			}
			else {
				result.put("code", 1);
				result.put("msg", "未获取到选项信息！");
			}
		} catch (Exception e) {
			result.put("code", ResponseJSON.FLAG_FAIL);
			result.put("msg", "出现异常，选项信息获取失败，请重试！");
			e.printStackTrace();
		}
		
		PrintWriter out = resp.getWriter();
		out.println(result);
		return null;
		
	}
	
}
